
#include "stdafx.h"
#include "DataLua.h"

namespace DataLua
{

bool push(lua_State* L, IData* pData)
{
	IData** pp = (IData**)lua_newuserdata(L, sizeof(IData*));
	*pp = pData;
	luaL_getmetatable(L, "woe.data");
	if (lua_istable(L, -1))
	{
		lua_setmetatable(L, -2);
		return true;
	}
	else
	{
		LOG_ERROR("Failed to push IData to lua, metatable not found!");
		return false;
	}
}

IData* getUserData(lua_State* L)
{
	IData**pp = (IData**)luaL_checkudata(L, 1, "woe.data");
	if (pp==NULL)
	{
		return NULL;
	}
	else
	{
		return *pp;
	}
}

int dump(lua_State* L)
{

	IData* pData = getUserData(L);

	if (pData)
	{
		std::cout<<"---------------------------------------"<<std::endl;
		std::cout<<"Group: "<<pData->getGroupID()<<"  ID: "<<pData->getID()<<std::endl;
		std::cout<<"DataX: "<<pData->getDataX()<<"  DataY: "<<pData->getDataY()<<"  DataZ: "<<pData->getDataZ()<<std::endl;
	}
	return 0;
}

int getGroupID(lua_State* L)
{
	IData* pData = getUserData(L);

	if (pData)
	{
		lua_pushinteger(L, pData->getGroupID());
	}
	else
	{
		lua_pushinteger(L, 0);
	}
	return 1;
}

int getID(lua_State* L)
{
	IData* pData = getUserData(L);

	if (pData)
	{
		lua_pushinteger(L, pData->getID());
	}
	else
	{
		lua_pushinteger(L, 0);
	}
	return 1;
}


int setDataX(lua_State* L)
{
	IData* pData = getUserData(L);
	if (pData)
	{
		pData->setDataX(lua_tointeger(L, 2));
	}
	return 0;
}

int getDataX(lua_State* L)
{
	IData* pData = getUserData(L);

	if (pData)
	{
		lua_pushinteger(L, pData->getDataX());
		return 1;
	}
	return 0;
}

int setDataY(lua_State* L)
{
	IData* pData = getUserData(L);
	if (pData)
	{
		pData->setDataY(lua_tointeger(L, 2));
	}
	return 0;
}

int getDataY(lua_State* L)
{
	IData* pData = getUserData(L);

	if (pData)
	{
		lua_pushinteger(L, pData->getDataY());
		return 1;
	}
	return 0;
}


int setDataZ(lua_State* L)
{
	IData* pData = getUserData(L);
	if (pData)
	{
		pData->setDataZ(lua_tointeger(L, 2));
	}
	return 0;
}

int getDataZ(lua_State* L)
{
	IData* pData = getUserData(L);

	if (pData)
	{
		lua_pushinteger(L, pData->getDataZ());
		return 1;
	}
	return 0;
}

int setData(lua_State* L)
{
	IData* pData = getUserData(L);
	if (pData)
	{
		pData->setData(lua_tointeger(L, 2), lua_tointeger(L, 3));
	}
	return 0;
}

int getData(lua_State* L)
{
	IData* pData = getUserData(L);

	if (pData)
	{
		lua_pushinteger(L, pData->getData(lua_tointeger(L, 2)));
		return 1;
	}
	return 0;
}



static const luaL_Reg data_funcs[] = {
	{"dump", dump},
	{"getID", getID},
	{"getGroupID", getGroupID},
	{"getX", getDataX},
	{"getY", getDataY},
	{"getZ", getDataZ},
	{"setX", setDataX},
	{"setY", setDataY},
	{"setZ", setDataZ},
	{"getData", getData},
	{"setData", setData},
	{NULL, NULL}
};


int libaray(lua_State* L)
{
	luaL_newmetatable(L, "woe.data");
	// fill member list into metatable
	luaL_register(L, NULL, data_funcs);

	// metatable.__index = metatable
	lua_pushstring(L, "__index");
	lua_pushvalue(L, -2);
	lua_settable(L, -3);

	return 0;
}

}
